<?php


namespace app\Activity\controller;


use app\AdminRest;
use app\activity\model\ActivityUserRecord as ActivityUserRecordModel;
use app\activity\model\ActivityItemRecord as ActivityItemRecordModel;
use app\activity\model\ActivityItemsInActivity as ActivityItemsInActivityModel;
use think\App;

class AdminActivityUserRecord extends AdminRest
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    public function getTableHead()
    {
        $param = $this->_param;

        if (!isset($param['id'])) {
            return $this->error("activity_id is required");
        }
        $activity_id = $param['id'];

        //准备表头
        $m_activity_item_in_activity = new ActivityItemsInActivityModel();

        $items_in_activity = $m_activity_item_in_activity->getListByFilter(['activity_id' => $activity_id]);

        $table_head = [
            ['label' => '微信昵称', 'prop' => 'nickName', 'type' => 'txt'],

            ['label' => '微信头像', 'prop' => 'avatarUrl', 'type' => 'img'],

            ['label' => '报名时间', 'prop' => 'create_time', 'type' => 'timestamp'],

            ['label' => '签到状态', 'prop' => 'is_sign_in', 'type' => 'txt'],
        ];

        foreach ($items_in_activity as $item) {

            $table_head[] = ['label' => $item['title'], 'prop' => (string)crc32($item['title']), 'type' => 'txt'];
        }

        $result['data'] = $table_head;

        return $this->success($result);

    }


    public function listByActivity()
    {
        $param = $this->_param;

        if (!isset($param['id']))
        {
            return $this->error("activity_id is required");
        }
        $activity_id = $param['id'];
        $page_config = array(
            'page' => 1,
            'page_count' => 20
        );
        //设置页码
        if (isset($param['page']) && $param['page'] > 0) $page_config['page'] = $param['page'];
        //设置每页的数据
        if (isset($param['page_count']) && $param['page_count'] > 0) $page_config['page_count'] = $param['page_count'];

        //准备分页数据
        $activity_user_records = [];

        $filter['uniacid'] = $this->_uniacid;

        $filter['activity_id'] = $activity_id;

        if(isset($param['is_sign_in'])&&$param['is_sign_in']!=3){

            $filter['is_sign_in'] = $param['is_sign_in'];
        }

        $where = [];

        //时间筛选
        if(!empty($param['start_time'])&&!empty($param['end_time'])){

            $start_time = $param['start_time'];

            $end_time   = $param['end_time']+86399;

            $where[] = ['create_time','between',"$start_time,$end_time"];
        }


        $model = new ActivityUserRecordModel();

        $page_config['total'] = $model->countByFilter($filter,$where);

        $records_tmp = $model->getListByFilter($filter, $page_config,$where);



        foreach ($records_tmp as $record) {
            $activity_user_records[$record['user_id']] =
                [
                    'user_id'     => $record['user_id'],
                    'create_time' => $record['create_time'],
                    'avatarUrl'   => !empty($record['userInfo']['avatarUrl'])?$record['userInfo']['avatarUrl']:'',
                    'nickName'    => !empty($record['userInfo']['nickName'])?$record['userInfo']['nickName']:'',
                    'is_sign_in'  => $record['is_sign_in']==1?'已签到':'未签到',
                    'id'  => $record['id']
                ];
        }

        $m_item_record = new ActivityItemRecordModel();

        $record_items = $m_item_record->getListByUserIdsAndActivityId(array_keys($activity_user_records), $activity_id);


//        dump($record_items);exit;
        foreach ($record_items as $item) {



            $activity_user_records[$item['user_id']][crc32($item['title'])] = $item['content'];
        }

        $page_config['total_page'] = (int)($page_config['total'] / $page_config['page_count']);
        if (($page_config['total'] % $page_config['page_count']) > 0) $page_config['total_page'] = $page_config['total_page'] + 1;
        //设置返回参数
        $result = $page_config;

        //返回数据
        $result['data'] = array_values($activity_user_records);

        return $this->success($result);

    }


}